﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Drawing;
using System.Web.Services;
using System.Windows.Forms;

public partial class APP_frmA_OrgTraineeInfoInput : System.Web.UI.Page
{
    DataTable dt1 = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = @"実習実施機関";
        ((APP_MasterPage_appCom)this.Master).changeTitle("他監理団体登録・参照");
        ((APP_MasterPage_appCom)this.Master).HideLiArea("frmA_OrgTraineeInfoInput");

        if (!IsPostBack)
        {
            if (Session["役"] != null)
            {
                if (Session["役"].ToString() == "1")
                {
                    hiden1.Value = "0";
                    ddl_企業.Focus();
                    bindDDL_企業();
                }
                else
                {
                    hiden1.Value = "1";
                    //ddl_企業.Visible = false;
                    txt_姓.Focus();

                }
                //ViewState.Remove("CurrentTable");
                Show_他団体実習生情報();
                bind国籍();
                DataColumnView();

            }
            else
            {
                Response.Redirect("~/frmD_Login.aspx");
            }
                //他団体実習生情報Manager frm_他団体実習生情報 = new 他団体実習生情報Manager();
            //dt1 = frm_他団体実習生情報.get他団体実習生情報(1);
            
        }
        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "regis", "Disable();hidenDIV();registerEnter();registerControlToValidation(\"validation\"); formatDate(\"css_text_calender\",\"/\");", true);
        //Show_他団体実習生情報();
        
        //validate氏.ErrorMessage = GetError("ER00001");
        //validate名.ErrorMessage = GetError("ER00001");
        //validate性別.ErrorMessage = GetError("ER00001");
        //validatetortextbox1.ErrorMessage = GetError("ER00001");
        //regula1.ErrorMessage = GetError("ER00002");
        //cs1.ErrorMessage = GetError("ER00002");
        //validate国籍.ErrorMessage = GetError("ER00001");
        //validate出生地.ErrorMessage = GetError("ER00001");
        //validatetextbox2.ErrorMessage = GetError("ER00001");
        //RegularExpressionValidator1.ErrorMessage = GetError("ER00002");
        //CustomValidator1.ErrorMessage = GetError("ER00002");
        //validatetextbox3.ErrorMessage = GetError("ER00001");
        //RegularExpressionValidator2.ErrorMessage = GetError("ER00002");
        //CustomValidator2.ErrorMessage = GetError("ER00002");
        validateddl_企業.ErrorMessage = GetError("ER00007");
        validatetxt_姓.ErrorMessage = GetError("ER00001");
        validatetxt_名.ErrorMessage = GetError("ER00001");
        validateddl_性別.ErrorMessage = GetError("ER00007");
        validatetextbox1.ErrorMessage = GetError("ER00001");
        CustomValidator4.ErrorMessage = GetError("ER00006");
        CustomValidator1.ErrorMessage = GetError("ER00002");
        validateddl_国籍.ErrorMessage = GetError("ER00007");
        validatetxt_出生地.ErrorMessage = GetError("ER00001");
        validatetextbox2.ErrorMessage = GetError("ER00001");
        CustomValidator2.ErrorMessage = GetError("ER00006");
        validatetextbox3.ErrorMessage = GetError("ER00001");
        CustomValidator3.ErrorMessage = GetError("ER00006");
        CustomValidator5.ErrorMessage = GetError("ER00005");
        CustomValidator6.ErrorMessage = GetError("ER00005");
        //btn_登録完了.Enabled = false;
    }



    public void bindDDL_企業()
    {
        本社情報Manager frm1 = new 本社情報Manager();
        DataTable dt1 = new DataTable();
        dt1 = frm1.Select_TraineeList();
        ddl_企業.DataSource = dt1;
        ddl_企業.DataTextField = "企業名";
        ddl_企業.DataValueField = "企業ID";
        ddl_企業.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_企業.Items.Insert(0, item);
    }
   
    
    protected void btn_登録完了_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataView dv = new DataView(dt);
            dv.Sort = "IsInit,ID DESC";

            他団体実習生情報Manager tns = new 他団体実習生情報Manager();
            if (ViewState["CurrentTable1"] != null)
            {


                foreach (DataRow dataRow1 in dv.Table.Rows)
                {
                    int id = Int32.Parse(dataRow1["IsInit"].ToString());
                    if (id == 0)
                    {
                        string 姓 = dataRow1["姓"].ToString();
                        string 名 = dataRow1["名"].ToString();
                        bool 性別 = Boolean.Parse(dataRow1["性別"].ToString());
                        DateTime 生年月日 = DateTime.Parse(dataRow1["生年月日"].ToString());
                        int 国籍 = Int32.Parse(dataRow1["国籍"].ToString());
                        string 出身地 = dataRow1["出身地"].ToString();
                        DateTime 入国年月日 = DateTime.Parse(dataRow1["入国年月日"].ToString());
                        DateTime 号移行年月日 = DateTime.Parse(dataRow1["2号移行年月日"].ToString());
                        int 企業ID = 2;// Session khi dang nhap
                        tns.Insert他団体実習生情報(姓, 名, 性別, 生年月日, 国籍, 出身地, 入国年月日, 号移行年月日, 企業ID);
                    }
                    if (id != 0 && Session["Array"] != null)
                    {
                        List<String> myAL1 = (List<String>)Session["Array"];
                        foreach (string str in myAL1)
                        {
                            int temp = Int32.Parse(dataRow1["ID"].ToString());
                            if (Int32.Parse(str) == temp)
                            {
                                string 姓 = dataRow1["姓"].ToString();
                                string 名 = dataRow1["名"].ToString();
                                bool 性別 = Boolean.Parse(dataRow1["性別"].ToString());
                                DateTime 生年月日 = DateTime.Parse(dataRow1["生年月日"].ToString());
                                int 国籍 = Int32.Parse(dataRow1["国籍"].ToString());
                                string 出身地 = dataRow1["出身地"].ToString();
                                DateTime 入国年月日 = DateTime.Parse(dataRow1["入国年月日"].ToString());
                                DateTime 号移行年月日 = DateTime.Parse(dataRow1["2号移行年月日"].ToString());
                                tns.Update他団体実習生情報(Int32.Parse(str), 姓, 名, 性別, 生年月日, 国籍, 出身地, 入国年月日, 号移行年月日);

                            }
                        }

                    }


                }



            }
            Show_他団体実習生情報();
            dt.Rows.Clear();
            //btn_登録完了.Enabled = false;
            Session.Remove("Array");
        }
        catch { }

    }


    protected void btn_リセット_Click(object sender, EventArgs e)
     {
         if (Session["temp"] != null)
         {
             string ids = Session["temp"].ToString();
             他団体実習生情報Manager frm = new 他団体実習生情報Manager();
             DataTable dt = new DataTable();
             dt = frm.Show_Update_他団体実習生情報(Int32.Parse(ids));
             if (dt.Rows.Count > 0)
             {
                 foreach (DataRow dataRow1 in dt.Rows)
                 {

                     string 姓 = dataRow1["姓"].ToString().Replace("&lt;", "<").ToString();
                     姓 = 姓.Replace("&gt;", ">");
                     string 名 = dataRow1["名"].ToString().Replace("&lt;", "<").ToString();
                     名 = 名.Replace("&gt;", ">");
                     txt_姓.Text = 姓;// dataRow1["姓"].ToString();
                     txt_名.Text = 名;//dataRow1["名"].ToString();
                     Boolean sex = Boolean.Parse(dataRow1["性別"].ToString());
                     if (sex == true)
                     {
                         ddl_性別.SelectedIndex = 2;
                     }
                     else
                     {
                         ddl_性別.SelectedIndex = 1;
                     }
                     string date = dataRow1["生年月日"].ToString();
                     DateTime date1 = DateTime.Parse(date);
                     date = date1.ToString("yyyy/MM/dd");
                     textbox1.Text = date.ToString();
                     ddl_国籍.SelectedIndex = int.Parse(dataRow1["国籍"].ToString());
                     string 出身地 = dataRow1["出身地"].ToString();
                     出身地 = 出身地.Replace("&lt;", "<");
                     出身地 = 出身地.Replace("&gt;", ">");
                     txt_出生地.Text = 出身地;
                     string 入国年月日 = dataRow1["入国年月日"].ToString();
                     DateTime 入国年月日1 = DateTime.Parse(入国年月日);
                     入国年月日 = 入国年月日1.ToString("yyyy/MM/dd");
                     textbox2.Text = 入国年月日;
                     string 号移行年月日 = dataRow1["2号移行年月日"].ToString();
                     DateTime 号移行年月日1 = DateTime.Parse(号移行年月日);
                     号移行年月日 = 号移行年月日1.ToString("yyyy/MM/dd");
                     textbox3.Text = 号移行年月日;
                 }

             }
         }
         else
         {
             //Session.Remove("temp");
             ResetForm();
         }

     }
    public void ResetForm()
    {
        txt_姓.Text = "";
        txt_名.Text = "";
        ddl_性別.SelectedIndex = 0;
        textbox1.Text = "";
        ddl_国籍.SelectedIndex = 0;
        txt_出生地.Text = "";
        textbox2.Text = "";
        textbox3.Text = "";
    }

    protected void btn_キャンセル_Click(object sender, EventArgs e)
    {
        Session.Remove("temp");
        Response.Redirect("frmA_MOtherOrgRef.aspx");
    }
    public void bind国籍()// hien thi quoc tich.
    {
        try
        {
            frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
            DataTable dt = frm.get国籍();
            ddl_国籍.DataSource = dt;
            ddl_国籍.DataTextField = "国籍名";
            ddl_国籍.DataValueField = "国籍名";
            ddl_国籍.DataBind();
            ListItem item = new ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_国籍.Items.Insert(0, item);
        }
        catch { }
    }
    public void Show_他団体実習生情報()// hien thi table 他団体実習生情報 trong gridview.
    {
        try
        {
            if (Session["役"].ToString() == "1")
            {
                他団体実習生情報Manager frm_他団体実習生情報 = new 他団体実習生情報Manager();
                int 企業ID = Int32.Parse(ddl_企業.SelectedValue.ToString()); // ID khi dang nhap cua cong ty tiep nhan
                dt1 = frm_他団体実習生情報.get他団体実習生情報(企業ID);
                DataColumn col = new DataColumn();
                col.DataType = System.Type.GetType("System.Int32");
                col.ColumnName = "IsInit";
                dt1.Columns.Add(col);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dt1.Rows[i]["IsInit"] = "2";// Nhung data da co trong database
                }


                if (dt1.Rows.Count > 0)
                {
                    grv.Visible = true;
                    grv1.Visible = false;
                    DataView dv = new DataView(dt1);
                    dv.Sort = "IsInit,入国年月日 DESC";
                    grv.DataSource = dv;

                    ViewState["CurrentTable"] = dt1;

                    grv.DataBind();

                }
                else
                {
                    ViewState["CurrentTable"] = dt1;
                    grv.Visible = false;
                    grv1.Visible = true;
                    AppCode_Common.ShowEmtyGrid<本社履歴>(grv1, 6, "データがありません。", "");

                }
            }
            else
            {
                他団体実習生情報Manager frm_他団体実習生情報 = new 他団体実習生情報Manager();
                int 企業ID = Int32.Parse(Session["企業コード_2"].ToString()); // ID khi dang nhap cua cong ty tiep nhan
                DataTable dt = new DataTable();
                dt = frm_他団体実習生情報.get他団体実習生情報(企業ID);
                DataColumn col = new DataColumn();
                col.DataType = System.Type.GetType("System.Int32");
                col.ColumnName = "IsInit";
                dt.Columns.Add(col);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["IsInit"] = "2";// Nhung data da co trong database
                }


                if (dt.Rows.Count > 0)
                {
                    grv1.Visible = false;
                    grv.Visible = true;
                    
                    DataView dv = new DataView(dt);
                    dv.Sort = "IsInit,入国年月日 DESC";
                    grv.DataSource = dv;

                    ViewState["CurrentTable"] = dt;

                    grv.DataBind();

                }
                else
                {
                    ViewState["CurrentTable"] = dt;
                    grv.Visible = false;
                    grv1.Visible = true;
                    AppCode_Common.ShowEmtyGrid<本社履歴>(grv1, 6, "データがありません。", "");

                }
            }
        }
        catch { }
         
    }
    public String GetError(string errorid)
    {
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        return dt.Rows[0][2].ToString();
    }

    protected void btn_次登録_Click(object sender, EventArgs e)
    {

        if (Session["役"].ToString() == "1")
        {
            他団体実習生情報Manager frm = new 他団体実習生情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Check他団体実習生情報(txt_姓.Text.ToString(), txt_名.Text.ToString(), DateTime.Parse(textbox2.Text.ToString()), Int32.Parse(ddl_企業.SelectedValue.ToString()));
            if (dt.Rows.Count > 0)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "alert();", true);

            }
            else
            {
                AddNewRow();
                ResetForm();
                txt_姓.Focus();
            }


        }
        else
        {
            他団体実習生情報Manager frm = new 他団体実習生情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Check他団体実習生情報(txt_姓.Text.ToString(), txt_名.Text.ToString(), DateTime.Parse(textbox2.Text.ToString()), Int32.Parse(Session["企業コード_2"].ToString()));
            if (dt.Rows.Count > 0)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "alert();", true);

            }
            else
            {
                AddNewRow();
                ResetForm();
                txt_姓.Focus();
            }
        }
    }



    protected void btDelete_Click(object sender, EventArgs e)
    {
        System.Web.UI.WebControls.Button bt = (System.Web.UI.WebControls.Button)sender;
        switch (bt.ID)
        {
            case "btDeleteYes": // they confirmed a delete
                //Response.Write("123");
                //label.Text = "123";
                if (Session["temp"] != null)
                {
                    ChecKConfirmUpdate();
                    ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "$.unblockUI();", true);
                }
                else
                {

                    AddNewRow();
                    ResetForm();
                    txt_姓.Focus();
                    ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "$.unblockUI();", true);
                }
                //mpe.Hide();
                break;

            case "btDeleteNo": // they clicked cancel
                //Response.Write("456");    
                // Do nothing
                //label.Text = "456";
                ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "$.unblockUI();", true);
                return;
                //mpe.Hide();
                break;

            default:
                throw new Exception("Unknow button click in btDelete_Click");
        }
        // clear selection and hide the confirm box
        
        
    }





    
    protected void btn_修正_Click(object sender, EventArgs e)
    {
        string ids = Session["temp"].ToString();
        if (Session["役"].ToString() == "1")
        {
            他団体実習生情報Manager frm = new 他団体実習生情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Check_Update_他団体実習生情報(txt_姓.Text.ToString(), txt_名.Text.ToString(), DateTime.Parse(textbox2.Text.ToString()), Int32.Parse(ddl_企業.SelectedValue.ToString()),Int32.Parse(ids));
            if (dt.Rows.Count > 0)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "alert();", true);

            }
            else
            {
                ChecKConfirmUpdate();
            }
        }
        else
        {
            他団体実習生情報Manager frm = new 他団体実習生情報Manager();
            DataTable dt = new DataTable();
            //foreach (DataRow row in dt.Rows)
            //{
            //    if (txt_姓.Text.ToString() == row["姓"].ToString() && ids != row["ID"].ToString())
            //    {
            //        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "alert();", true);
            //    }
            //    else
            //    {
            //        ChecKConfirmUpdate();
            //    }
            //}
            dt = frm.Check_Update_他団体実習生情報(txt_姓.Text.ToString(), txt_名.Text.ToString(), DateTime.Parse(textbox2.Text.ToString()), Int32.Parse(Session["企業コード_2"].ToString()), Int32.Parse(ids));
            if (dt.Rows.Count > 0)
            {

                ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "a", "alert();", true);

            }
            else
            {
                ChecKConfirmUpdate();
            }
        }
    }


    public void ChecKConfirmUpdate()
    {
        try
        {
            btn_次登録.Visible = true;
            btn_修正.Visible = false;
            他団体実習生情報Manager tns = new 他団体実習生情報Manager();
            string 性別 = "";
            string ids;
            ids = Session["temp"].ToString();
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            foreach (DataRow row1 in dtCurrentTable.Rows)
            {
                int IDtam = Int32.Parse(row1["ID"].ToString());
                if (Int32.Parse(ids) == IDtam)
                {
                    //string aa = row1["姓"].ToString().Replace("&lt;", "<").ToString();
                    string 姓 = txt_姓.Text.ToString().Trim();
                    姓 = 姓.Replace("<", "&lt;");
                    姓 = 姓.Replace(">", "&gt;");
                    row1["姓"] = 姓;// txt_姓.Text.ToString();

                    string 名 = txt_名.Text.ToString().Trim();
                    名 = 名.Replace("<", "&lt;");
                    名 = 名.Replace(">", "&gt;");
                    row1["名"] = 名;
                    if (ddl_性別.SelectedItem.Text.Equals("男"))
                    {
                        性別 = "False";
                        row1["性別"] = 性別;
                    }
                    else
                    {
                        性別 = "True";
                        row1["性別"] = 性別;
                    }
                    row1["生年月日"] = textbox1.Text.ToString();
                    frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
                    国籍 country = new 国籍();
                    country.国籍名 = ddl_国籍.SelectedItem.Text.ToString();
                    DataTable dt1 = frm.get国籍ID(country);
                    int 国籍名 = Int32.Parse(dt1.Rows[0][0].ToString());
                    row1["国籍"] = 国籍名;
                    row1["国籍名"] = ddl_国籍.SelectedItem.Text;
                    string 出身地 = txt_出生地.Text.ToString().Trim();
                    出身地 = 出身地.Replace("<", "&lt;");
                    出身地 = 出身地.Replace(">", "&gt;");
                    row1["出身地"] = 出身地;
                    row1["入国年月日"] = textbox2.Text.ToString();
                    row1["2号移行年月日"] = textbox3.Text.ToString();
                    if (Convert.ToInt32(row1["IsInit"]) != 0)
                    {
                        row1["IsInit"] = "1";
                    }
                    tns.Update他団体実習生情報(Int32.Parse(ids), 姓, 名, Boolean.Parse(性別), DateTime.Parse(textbox1.Text.ToString()), 国籍名, 出身地, DateTime.Parse(textbox2.Text.ToString()), DateTime.Parse(textbox3.Text.ToString()));
                }
            }
            ViewState["CurrentTable"] = dtCurrentTable;
            DataView dv = new DataView(dtCurrentTable);

            dv.Sort = "IsInit,入国年月日 DESC";
            grv.DataSource = dv;
            grv.DataBind();
            ResetForm();
            Session.Remove("temp");
            //btn_登録完了.Enabled = true;
        }
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "regis", "Disable();hidenDIV();registerEnter();registerControlToValidation(\"validation\"); formatDate(\"css_text_calender\",\"/\");", true);
    }



    protected void AddNewRow()// them moi 1 record trong bang tam.
    {
        try
        {
            
                if (Session["役"].ToString() == "1")
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    DataRow drCurrentRow = null;
                    他団体実習生情報Manager tns = new 他団体実習生情報Manager();
                    int 企業ID = Int32.Parse(ddl_企業.SelectedValue.ToString()); // ID khi dang nhap cua cong ty tiep nhan
                    string 性別 = "";
                    //foreach (DataRow row in dtCurrentTable.Rows)
                    //{

                    //if (dtCurrentTable.Rows.Count > 0)
                    //{
                    drCurrentRow = dtCurrentTable.NewRow();
                    string 姓 = txt_姓.Text.ToString().Trim();
                    姓 = 姓.Replace("<", "&lt;");
                    姓 = 姓.Replace(">", "&gt;");
                    drCurrentRow["姓"] = 姓;
                    string 名 = txt_名.Text.ToString().Trim();
                    名 = 名.Replace("<", "&lt;");
                    名 = 名.Replace(">", "&gt;");
                    drCurrentRow["名"] = 名;
                    if (ddl_性別.SelectedItem.Text.Equals("男"))
                    {
                        性別 = "False";
                        drCurrentRow["性別"] = 性別;
                    }
                    else
                    {
                        性別 = "True";
                        drCurrentRow["性別"] = 性別;
                    }
                    drCurrentRow["生年月日"] = textbox1.Text.ToString();
                    frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
                    国籍 country = new 国籍();
                    country.国籍名 = ddl_国籍.SelectedItem.Text.ToString();
                    DataTable dt1 = frm.get国籍ID(country);
                    int 国籍名 = Int32.Parse(dt1.Rows[0][0].ToString());
                    drCurrentRow["国籍"] = 国籍名;
                    drCurrentRow["国籍名"] = ddl_国籍.SelectedItem.Text;
                    string 出身地 = txt_出生地.Text.ToString().Trim();
                    出身地 = 出身地.Replace("<", "&lt;");
                    出身地 = 出身地.Replace(">", "&gt;");
                    drCurrentRow["出身地"] = 出身地;
                    drCurrentRow["入国年月日"] = textbox2.Text.ToString();
                    drCurrentRow["2号移行年月日"] = textbox3.Text.ToString();
                    drCurrentRow["IsInit"] = "0"; // Set IsInit cho nhung dong moi them vao
                    DataTable dt2 = new DataTable();
                    dt2 = tns.Insert他団体実習生情報(姓, 名, Boolean.Parse(性別), DateTime.Parse(textbox1.Text.ToString()), 国籍名, 出身地, DateTime.Parse(textbox2.Text.ToString()), DateTime.Parse(textbox3.Text.ToString()), 企業ID);

                    drCurrentRow["ID"] = dt2.Rows[0][0].ToString();
                    dtCurrentTable.Rows.Add(drCurrentRow);

                    //tns.Insert他団体実習生情報(姓, 名, Boolean.Parse(性別), DateTime.Parse(textbox1.Text.ToString()), 国籍名, 出身地, DateTime.Parse(textbox2.Text.ToString()), DateTime.Parse(textbox3.Text.ToString()), 企業ID);

                    ViewState["CurrentTable"] = dtCurrentTable;
                    DataView dv = new DataView(dtCurrentTable);

                    dv.Sort = "IsInit,入国年月日 DESC";
                    grv.Visible = true;
                    grv1.Visible = false;
                    grv.DataSource = dv;
                    grv.DataBind();


                }
                else
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    DataRow drCurrentRow = null;
                    他団体実習生情報Manager tns = new 他団体実習生情報Manager();
                    int 企業ID = Int32.Parse(Session["企業コード_2"].ToString()); // ID khi dang nhap cua cong ty tiep nhan
                    string 性別 = "";
                    //foreach (DataRow row in dtCurrentTable.Rows)
                    //{

                    //if (dtCurrentTable.Rows.Count > 0)
                    //{
                    drCurrentRow = dtCurrentTable.NewRow();
                    string 姓 = txt_姓.Text.ToString().Trim();
                    姓 = 姓.Replace("<", "&lt;");
                    姓 = 姓.Replace(">", "&gt;");
                    drCurrentRow["姓"] = 姓;
                    string 名 = txt_名.Text.ToString().Trim();
                    名 = 名.Replace("<", "&lt;");
                    名 = 名.Replace(">", "&gt;");
                    drCurrentRow["名"] = 名;
                    if (ddl_性別.SelectedItem.Text.Equals("男"))
                    {
                        性別 = "False";
                        drCurrentRow["性別"] = 性別;
                    }
                    else
                    {
                        性別 = "True";
                        drCurrentRow["性別"] = 性別;
                    }
                    drCurrentRow["生年月日"] = textbox1.Text.ToString();
                    frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
                    国籍 country = new 国籍();
                    country.国籍名 = ddl_国籍.SelectedItem.Text.ToString();
                    DataTable dt1 = frm.get国籍ID(country);
                    int 国籍名 = Int32.Parse(dt1.Rows[0][0].ToString());
                    drCurrentRow["国籍"] = 国籍名;
                    drCurrentRow["国籍名"] = ddl_国籍.SelectedItem.Text;
                    string 出身地 = txt_出生地.Text.ToString().Trim();
                    出身地 = 出身地.Replace("<", "&lt;");
                    出身地 = 出身地.Replace(">", "&gt;");
                    drCurrentRow["出身地"] = 出身地;
                    drCurrentRow["入国年月日"] = textbox2.Text.ToString();
                    drCurrentRow["2号移行年月日"] = textbox3.Text.ToString();
                    drCurrentRow["IsInit"] = "0"; // Set IsInit cho nhung dong moi them vao
                    DataTable dt2 = new DataTable();
                    dt2 = tns.Insert他団体実習生情報(姓, 名, Boolean.Parse(性別), DateTime.Parse(textbox1.Text.ToString()), 国籍名, 出身地, DateTime.Parse(textbox2.Text.ToString()), DateTime.Parse(textbox3.Text.ToString()), 企業ID);
                    
                    drCurrentRow["ID"] = dt2.Rows[0][0].ToString();
                    dtCurrentTable.Rows.Add(drCurrentRow);

                    //tns.Insert他団体実習生情報(姓, 名, Boolean.Parse(性別), DateTime.Parse(textbox1.Text.ToString()), 国籍名, 出身地, DateTime.Parse(textbox2.Text.ToString()), DateTime.Parse(textbox3.Text.ToString()), 企業ID);

                    ViewState["CurrentTable"] = dtCurrentTable;
                    DataView dv = new DataView(dtCurrentTable);

                    dv.Sort = "IsInit,入国年月日 DESC";
                    grv.Visible = true;
                    grv1.Visible = false;
                    grv.DataSource = dv;
                    grv.DataBind();
                }
            }
            

        
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "regis", "Disable();hidenDIV();registerEnter();registerControlToValidation(\"validation\"); formatDate(\"css_text_calender\",\"/\");", true);

    }

    public string ConvertString(string str)// convert chuoi
    {
        if(str.Equals("True"))
            return "女";
        else return "男";
    }
    public void DataColumnView()// tao 1 bang tam chua record them moi vao ma chua vao DB.
    {
        try
        {
            DataTable dtv = new DataTable();
            DataColumn col;

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "姓";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "名";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.Boolean");
            col.ColumnName = "性別";
            dtv.Columns.Add(col);


            col = new DataColumn();
            col.DataType = System.Type.GetType("System.DateTime");
            col.ColumnName = "生年月日";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.Int32");
            col.ColumnName = "国籍";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "出身地";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.DateTime");
            col.ColumnName = "入国年月日";
            dtv.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.DateTime");
            col.ColumnName = "2号移行年月日";
            dtv.Columns.Add(col);

            ViewState["CurrentTable1"] = dtv;

        }
        catch { }

       
    }
    public void DataCurrentTable()// bang tam chua record moi va cu trong DB.
    {
        try
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable1"];
                DataRow drCurrentRow = null;

                drCurrentRow = dt.NewRow();
                drCurrentRow["姓"] = txt_姓.Text.ToString();
                drCurrentRow["名"] = txt_名.Text.ToString();
                if (ddl_性別.SelectedItem.Text.Equals("男"))
                {
                    string a = "False";
                    drCurrentRow["性別"] = a;
                }
                else
                {
                    string a = "True";
                    drCurrentRow["性別"] = a;
                }
                drCurrentRow["生年月日"] = textbox1.Text.ToString();
                dt.Rows.Add(drCurrentRow);

                frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
                国籍 country = new 国籍();
                country.国籍名 = ddl_国籍.SelectedItem.Text.ToString();
                DataTable dt1 = frm.get国籍ID(country);
                int 国籍名 = Int32.Parse(dt1.Rows[0][0].ToString());
                drCurrentRow["国籍"] = 国籍名;
                drCurrentRow["出身地"] = txt_出生地.Text.ToString();
                drCurrentRow["入国年月日"] = textbox2.Text.ToString();
                drCurrentRow["2号移行年月日"] = textbox3.Text.ToString();

                ViewState["CurrentTable1"] = dt;

            }
        }
        catch { }
        
    }
          
    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.Cells[7].Text.ToString() != "&nbsp;")
            {
                int ID = Int32.Parse(e.Row.Cells[7].Text);
                if (ID == 0)
                {
                    e.Row.CssClass = "insert";
                }
                else if (ID == 1)
                {
                    e.Row.CssClass = "update";
                }
            }
                
        }
    }

    protected void ItemView_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
       
        try
        {
            if(e.CommandName.Equals("LoadUpdate"))
            {
            int ID = Int32.Parse(e.CommandArgument.ToString());
            Session["temp"] = ID.ToString();
            System.Collections.Generic.List<String> myAL = new List<string>();
            if (Session["Array"] != null)
            {
                myAL = (System.Collections.Generic.List<String>)Session["Array"];

            }
            myAL.Add(ID.ToString());
            Session["Array"] = myAL;
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if(dt.Rows.Count > 0)
            {
                foreach (DataRow dataRow1 in dt.Rows)
                {
                    int IDtam = Int32.Parse(dataRow1["ID"].ToString());
                    if (ID == IDtam)
                    {
                        string 姓 = dataRow1["姓"].ToString().Replace("&lt;", "<").ToString();
                        姓 = 姓.Replace("&gt;", ">");
                        string 名 = dataRow1["名"].ToString().Replace("&lt;", "<").ToString();
                        名 = 名.Replace("&gt;", ">");
                        txt_姓.Text = 姓;// dataRow1["姓"].ToString();
                        txt_名.Text = 名;//dataRow1["名"].ToString();
                        Boolean sex = Boolean.Parse(dataRow1["性別"].ToString());
                        if (sex == true)
                        {
                            ddl_性別.SelectedIndex = 2;
                        }
                        else
                        {
                            ddl_性別.SelectedIndex = 1;
                        }
                        string date = dataRow1["生年月日"].ToString();
                        DateTime date1 = DateTime.Parse(date);
                        date = date1.ToString("yyyy/MM/dd");
                        textbox1.Text = date.ToString();
                        ddl_国籍.SelectedIndex = int.Parse(dataRow1["国籍"].ToString());
                        string 出身地 = dataRow1["出身地"].ToString();
                        出身地 = 出身地.Replace("&lt;", "<");
                        出身地 = 出身地.Replace("&gt;", ">");
                        txt_出生地.Text = 出身地;
                        string 入国年月日 = dataRow1["入国年月日"].ToString();
                        DateTime 入国年月日1 = DateTime.Parse(入国年月日);
                        入国年月日 = 入国年月日1.ToString("yyyy/MM/dd");
                        textbox2.Text = 入国年月日;
                        string 号移行年月日 = dataRow1["2号移行年月日"].ToString();
                        DateTime 号移行年月日1 = DateTime.Parse(号移行年月日);
                        号移行年月日 = 号移行年月日1.ToString("yyyy/MM/dd");
                        textbox3.Text = 号移行年月日;
                    }
                }
            }
            btn_修正.Visible = true;
            btn_次登録.Visible = false;

            //Response.Redirect("frmA_OrgTraineeInfoInput.aspx");
        }
        }
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "regis", "Disable();hidenDIV();registerEnter();registerControlToValidation(\"validation\"); formatDate(\"css_text_calender\",\"/\");", true);
    }

    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)// phan trang.
    {
        grv.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        DataView dv = new DataView(dt);
        dv.Sort = "IsInit,入国年月日 DESC";
        grv.DataSource = dv;
        //grv.DataSource = dt;
        grv.DataBind();
    
    }

    protected void ddl_企業_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (Session["役"].ToString() == "1")
            {
                他団体実習生情報Manager frm_他団体実習生情報 = new 他団体実習生情報Manager();
                int 企業ID = Int32.Parse(ddl_企業.SelectedValue.ToString()); // ID khi dang nhap cua cong ty tiep nhan
                dt1 = frm_他団体実習生情報.get他団体実習生情報(企業ID);
                DataColumn col = new DataColumn();
                col.DataType = System.Type.GetType("System.Int32");
                col.ColumnName = "IsInit";
                dt1.Columns.Add(col);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dt1.Rows[i]["IsInit"] = "2";// Nhung data da co trong database
                }


                if (dt1.Rows.Count > 0)
                {
                    grv.Visible = true;
                    grv1.Visible = false;
                    DataView dv = new DataView(dt1);
                    dv.Sort = "IsInit,入国年月日 DESC";
                    grv.DataSource = dv;

                    ViewState["CurrentTable"] = dt1;

                    grv.DataBind();

                }
                else
                {
                    ViewState["CurrentTable"] = dt1;
                    grv.Visible = false;
                    grv1.Visible = true;
                    AppCode_Common.ShowEmtyGrid<本社履歴>(grv1, 6, "データがありません。", "");

                }
            }
        }
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "regis", "Disable();hidenDIV();registerEnter();registerControlToValidation(\"validation\"); formatDate(\"css_text_calender\",\"/\");", true);
    }

    public string ShowTextLength1(string temp)
    {
        //tieng viet
        if (temp.Length > 20)
            temp = temp.Substring(0, 20) + "...";
        if (temp.Length == 0)
        {
            temp = "";
        }
        return temp;

    }
}
